package com.tonicgb.shared.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @author zhaiyujin
 * @date 2021-09-27 19:35:13
 * @说明  阿里数据库连接池 Druid配置
 */
@Configuration
public class DruidConfiguration {

    //绑定datasource数据源
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){

        return new DruidDataSource();
    }

    //后台检控
    //因为springboot 内置了servlet容器，所以没有web.xml
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //配置访问后台页面地址 /druid/*
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        HashMap<String, String> init = new HashMap<>();

        //设置登陆信息
        //用户名loginUsername和loginPassword是固定的key，不能修改，只能修改value
        init.put("loginUsername","root");
        init.put("loginPassword","123456");

        //如果allow值为空，则所有人都可以访问
        init.put("allow","");

        //设置后台登陆信息，最后将集合作为参数传入
        bean.setInitParameters(init);
        return bean;
    }
}

